using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace OpenCvTesting
{
	static class BitmapCreationHelper
	{
		public static Bitmap GetBitmapFromPixelData(byte[] imageData, int width, int height)
		{
			Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed);

			ColorPalette palette = bitmap.Palette;
			for (int i = 0; i < 256; i++)
			{
				palette.Entries[i] = Color.FromArgb(255, i, i, i);
			}
			bitmap.Palette = palette;

			Rectangle BoundsRect = new Rectangle(0, 0, width, height);
			BitmapData bmpData = bitmap.LockBits(BoundsRect,
											ImageLockMode.WriteOnly,
											bitmap.PixelFormat);

			IntPtr ptr = bmpData.Scan0;

			int bytes = bmpData.Stride * bitmap.Height;
			//byte[] rgbValues = new byte[bytes];

			// fill in rgbValues, e.g. with a for loop over an input array

			Marshal.Copy(imageData, 0, ptr, bytes);
			bitmap.UnlockBits(bmpData);
			return bitmap;
		}
	}
}
